package com.sghd.candy.module.http.web;

import com.sghd.candy.module.account.service.AccountService;
import com.sghd.candy.module.charge.service.ChargeService;
import com.sghd.candy.module.config.service.ConfigService;
import com.sghd.common.socket.session.Session;
import com.sghd.common.socket.session.SessionManager;
import com.sghd.common.utils.json.JsonUtils;
import com.sghd.pojo.candy.charge.dto.ChargeDto;
import com.sghd.pojo.fight.game.Result;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Allen
 * @date 2019/01/07
 */
@RestController
public class MaintainController {
    private static final Logger logger = LoggerFactory.getLogger(MaintainController.class);
    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private ConfigService configService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private ChargeService chargeService;
    /**
     * 踢人
     * @param playerIds
     * @return
     */
    @RequestMapping(value = "/kick", method={RequestMethod.POST})
    public Result kick(@RequestParam("playerIds") String playerIds) {
        if(StringUtils.isNotEmpty(playerIds)){
            List<Long> ids = JsonUtils.string2Collection(playerIds, ArrayList.class, Long.class);
            for (Long playerId : ids) {
                sessionManager.kick(Session.KICK, playerId);
            }
        }
        return Result.SUCCESS();
    }

    /**
     * 玩家冻结/解除操作
     * @param playerIds
     * @param freeze
     * @return
     */
    @RequestMapping(value = "/freeze")
    public Result freeze(String playerIds, boolean freeze) {
        if(StringUtils.isNotEmpty(playerIds)){
            List<Long> ids = JsonUtils.string2Collection(playerIds, ArrayList.class, Long.class);
            for (Long playerId : ids) {
                if (freeze) {
                    accountService.block(playerId);
                    sessionManager.kick(Session.KICK, playerId);
                } else {
                    accountService.deblock(playerId);
                }
            }
        }
        return Result.SUCCESS();
    }

    /**
     * 获得在线人数

       */
    @RequestMapping(value = "/onlineCount")
    public Result<Integer> getOnlineCount() {
        return Result.valueOf(0, sessionManager.getOnlineIdentities().size());
    }

    /**
     * 重新加载数据库配置文件
     * @param tableName
     * @return
     */
    @RequestMapping(value = "/reload")
    public Result reloadConfig(String tableName) {
        configService.reload(tableName);
        return Result.SUCCESS();
    }

    @RequestMapping(value = "/charge", method={RequestMethod.POST})
    public com.sghd.common.utils.model.Result charge(@RequestBody String params) {
        try {
            ChargeDto chargeDto = JsonUtils.string2Object(params, ChargeDto.class);
            return chargeService.charge(chargeDto);
        } catch (Exception e) {
            logger.error("charge", e);
            return com.sghd.common.utils.model.Result.ERROR(-1);
        }
    }
}
